<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="收费站" prop="deptId" v-if="deptId==101">
        <el-tree-select v-model="queryParams.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
          class="margin-right" placeholder="请选择部门" check-strictly clearable @change="bindChangeTree($event,'deptId')" />
      </el-form-item>
      <el-form-item label="上传时间" prop="reportTime">
        <el-date-picker clearable v-model="queryParams.reportTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择上传时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="上传人" prop="reporter">
        <el-input v-model="queryParams.reporter" placeholder="请输入上传人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['coordination:lightspot:add']">新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['coordination:lightspot:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="lightspotList" @selection-change="handleSelectionChange" show-overflow-tooltip>
      <el-table-column type="index" width="65" align="center" label="序号" />
      <el-table-column label="收费站" align="center" prop="deptName" v-if="deptId==101" />
      <el-table-column label="工作内容" align="center" prop="content" />
      <el-table-column label="工作总结" align="center" prop="summary">
      </el-table-column>
      <el-table-column label="印证资料" align="center" prop="proof" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.proof" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="上传时间" align="center" prop="reportTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.reportTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上传人" align="center" prop="reporterName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleUpdate(scope.row,1)" >详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['coordination:lightspot:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['coordination:lightspot:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改本月工作亮点对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="lightspotRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="收费站" prop="deptId">
          <el-tree-select style="width: 100%;" v-model="form.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }"
            value-key="id" class="margin-right" placeholder="请选择部门" check-strictly clearable   />
        </el-form-item>
        <el-form-item label="工作内容">
          <el-input v-model="form.content" type="textarea" placeholder="请输入工作内容" rows="6"  :disabled="isShow" />
        </el-form-item>
        <el-form-item label="工作总结" prop="summary">
          <el-input v-model="form.summary" type="textarea" placeholder="请输入工作内容" rows="6"  :disabled="isShow" />
        </el-form-item>
        <el-form-item label="印证资料" prop="proof">
          <image-upload v-model="form.proof"  :disabled="isShow" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" rows="6" :disabled="isShow" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm" v-if="!isShow">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <Viewer ref="fileViewerRef" :dialog-doc="dialogDoc" />
  </div>
</template>

<script setup name="Lightspot">
import { listLightspot, getLightspot, delLightspot, addLightspot, updateLightspot } from "@/api/coordination/lightspot";
import { deptTreeSelect } from "@/api/system/user";
import { getCKInfo } from '@/utils/auth'
import Viewer from "@/components/Custom/viewer.vue";
const { proxy } = getCurrentInstance();

const baseUrl = import.meta.env.VITE_APP_BASE_API;
const lightspotList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const deptOptions = ref([])
const deptId = ref(Number(getCKInfo('deptId')))
const fileViewerRef = ref()
const dialogDoc = ref(false)
const isShow=ref(false)
const data = reactive({
  form: { deptId: deptId.value, },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deptId: deptId.value,
    reportTime: null,
    reporter: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询本月工作亮点列表 */
function getList() {
  loading.value = true;
  listLightspot(queryParams.value).then(response => {
    lightspotList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
};
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    deptId: deptId.value,
    content: null,
    summary: null,
    proof: null,
    reportTime: null,
    reporter: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("lightspotRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;isShow.value=false;
  title.value = "添加本月工作亮点";
}

/** 修改按钮操作 */
function handleUpdate(row,type) {
  reset();
  const _id = row.id || ids.value
  getLightspot(_id).then(response => {
    form.value = response.data;
    open.value = true;
    if(type==1){
      isShow.value=true;
      title.value = "查看本月工作亮点";
    }else{
      isShow.value=false;
      title.value = "修改本月工作亮点";
    }
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["lightspotRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateLightspot(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addLightspot(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除本月工作亮点编号为"' + _ids + '"的数据项？').then(function () {
    return delLightspot(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('coordination/lightspot/export', {
    ...queryParams.value
  }, `工作亮点_${new Date().getTime()}.xlsx`)
}
// 预览
const previewFun = (file) => {
  dialogDoc.value = true;
  let data = {};
  // 上传过的文件组成完成的网络路径
  if (file&&file.indexOf(',')==-1) {
    data.src = `${baseUrl}${file}`;
  }else if(file&&file.indexOf(',')!=-1){
    let src=file.split(',');
    let srcArr=[]
    src.forEach(item=>{srcArr.push(`${baseUrl}${item}`)})
    data.src = srcArr.join(',');
    file=srcArr[0]
  }

  const arr = file.split(".");
  const suffix = arr[arr.length - 1];
  if (suffix == "docx" || suffix == "doc") {
    fileViewerRef.value?.viewDocx(data, '重点工作');
  } else if (suffix == "xlsx" || suffix == "xls") {
    fileViewerRef.value?.viewXlsx(data);
  } else if (suffix == "jpg" || suffix == "jpeg" || suffix == "png") {
    fileViewerRef.value?.viewImg(data);
  } else if (suffix == "pdf") {
    fileViewerRef.value?.viewPdf(data);
  }
};
getList();
getDeptTree()
</script>
